package com.fanbai.score.user.http;

import android.content.Context;

import com.fanbai.score.common.Utils.BitmapUtil;
import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.common.http.HttpUtils;
import com.fanbai.score.common.http.WebCodes;
import com.fanbai.score.user.bean.HeadImage_Bean;
import com.fanbai.score.user.bean.NoticeInfo_Bean;
import com.fanbai.score.user.bean.User_Bean;
import com.tencent.bugly.crashreport.CrashReport;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by acer on 2017/6/3.
 */

public class UserRequestNetwork_Utils {

    /**
     * 获取个人信息
     * @param ctx
     * @return
     */
    public static void getUserInfo(final Context ctx, final CallBack<User_Bean> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getuserinfo");
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    User_Bean bean = JsonUtils.toBean(body, User_Bean.class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, bean);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 登陆
     * @param ctx
     * @param mobile
     * @param passwd
     * @param callBack
     */
    public static void login(final Context ctx, String mobile, String passwd, final CallBack<User_Bean> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "login");
        params.put("mobile", mobile);
        params.put("passwd", passwd);
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    User_Bean bean = JsonUtils.toBean(body, User_Bean.class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, bean);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }
    /**
     * 注册
     * @param ctx
     * @return
     */
    public static void userReg(final Context ctx, String mobile, String passwd, String vcode, final CallBack<String> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "userreg");
        params.put("mobile", mobile);
        params.put("passwd", passwd);
        params.put("vcode", vcode);
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, body);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 验证码发送
     * @param ctx
     * @param mobile
     * @param subtype reg or resetpwd
     * @param callBack
     */
    public static void sendVcode(final Context ctx, String mobile, String subtype, final CallBack<String> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "sendvcode");
        params.put("mobile", mobile);
        params.put("subtype", subtype);//reg or resetpwd
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, body);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 密码重置
     * @param ctx
     * @param mobile
     * @param passwd
     * @param vcode
     * @param callBack
     */
    public static void forgotPwd(final Context ctx, String mobile, String passwd, String vcode, final CallBack<String> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "forgotpwd");
        params.put("mobile", mobile);
        params.put("passwd", passwd);
        params.put("vcode", vcode);
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, body);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 获取公告
     * @param ctx
     * @param callBack
     */
    public static void getNoticeInfos(final Context ctx, final CallBack<List<NoticeInfo_Bean>> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getnoticeinfos ");
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    List<NoticeInfo_Bean> beans = JsonUtils.toArr(body, NoticeInfo_Bean[].class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, beans);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 获取首页头部信息
     * @param ctx
     * @param callBack
     */
    public static void getHeadImages(final Context ctx, final CallBack<List<HeadImage_Bean>> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getheadimages");
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    List<HeadImage_Bean> beans = JsonUtils.toArr(body, HeadImage_Bean[].class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, beans);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 获取银行卡信息
     * @param ctx
     * @param callBack
     */
    public static void getUserBankInfos(final Context ctx, final CallBack<String> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getuserbankinfos");
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, body);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 今日兑换值
     * @param ctx
     * @param callBack
     */
    public static void getBaseValue(final Context ctx, final CallBack<String> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getbasevalue");
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, body);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 活动中兑换邀请码开关
     * @param ctx
     * @param callBack
     */
    public static void inviteCodeStart(final Context ctx, final CallBack<String> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "invitecodestart");
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, body);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 意见反馈
     * @param ctx
     * @param context
     * @param callBack
     */
    public static void feedback(final Context ctx, String context, final CallBack<String> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "feedback");
        params.put("feedback", context);
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, body);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 修改昵称
     * @param ctx
     * @param nickname
     * @param callBack
     */
    public static void updatenickname(final Context ctx, String nickname, final CallBack<String> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "updatenickname");
        params.put("nickname", nickname);
        String url = WebCodes.HTTP_URL_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, body);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }


    /**
     * 上传图片
     *
     * @param ctx
     * @param imagePath
     * @param callBack
     */
    public static void editUserIcon(final Context ctx, String imagePath, final HttpUtils.UploadCallBack callBack) {

        File file = new File(BitmapUtil.compressPicture_Path(imagePath));
        Map<String, String> params = new HashMap<String, String>();
        params.put("type", "uploadlogo");
        String url = WebCodes.HTTP_URL_USER;
        HttpUtils.upload(ctx, url, file, params, callBack);
    }
}
